﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system.baseset.basedata
{
    public partial class department_list : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string departmentremark;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                departmentremark = Request.Params["departmentremark"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            string error_msg = "";

            //构造SQL语句
            string sql = @"select a.departmentid, a.departmentremark, a.sysdate
                            from department a
                              where 1=1";

            sql += (departmentremark == "") ? "" : String.Format(" and departmentremark like '%{0}%'", departmentremark);

            Execute exec = new Execute(Session["connectionString"].ToString());

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return null;
            }

            DataSet dsResult = new DataSet();

            //绑定菜单
            DataTable dt = new DataTable("List");

            dt.Columns.Add("dgvID");
            dt.Columns.Add("departmentid");
            dt.Columns.Add("departmentremark");
            dt.Columns.Add("sysdate");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["departmentid"] = dr["departmentid"].ToString();
                drnew["departmentremark"] = dr["departmentremark"].ToString();
                drnew["sysdate"] = dr["sysdate"].ToString();

                dt.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dt);

            return dsResult;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void bind()
        {
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            rplist.DataSource = ds.Tables[0];
            rplist.DataBind();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        private void delete(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return;
            }

            alert_msg = "";

            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //删除department中数据
                sql = String.Format("delete from department where departmentid='{0}'", id);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Delete);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        private void edit(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return;
            }

            GetParma();

            string url = String.Format("/system/baseset/basedata/department_edit.aspx?id={0}&departmentremark={1}", id, departmentremark);

            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //查询
                bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string url = "/system/baseset/basedata/department.aspx";

            Response.Redirect(url);
        }

        //导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //取得数据
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            //构造导出表头
            String[] headname = { "序号", "部门编码", "部门描述", "创建时间" };

            //导出
            ExportToFile.ExportToExcel(ds.Tables[0], HttpContext.Current.Response, headname);
        }

        //删除与编辑
        protected void rplist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            switch (e.CommandName)
            {
                case "Delete":
                    {
                        delete(id);

                        //查询
                        bind();

                        break;
                    }
                case "Edit":
                    {
                        edit(id);

                        //查询
                        bind();

                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
}